﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyTools;

namespace LimsFormRequest2
{
    public partial class Form1 : Form
    {
        public static Dictionary<string, string> evn = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();

            evn.Add("本地", "http://localhost:8001/LIMS-Server/MPLM_Service_Gateway/");
            evn.Add("SIT", "https://limssit.midea.com/LIMS-Server/MPLM_Service_Gateway/");
            evn.Add("UAT", "https://limsuat.midea.com/LIMS-Server/MPLM_Service_Gateway/");
            evn.Add("VER", "https://limsver.midea.com/LIMS-Server/MPLM_Service_Gateway/");
            evn.Add("PROD", "https://lims.midea.com/LIMS-Server/MPLM_Service_Gateway/");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach(KeyValuePair<string,string> kv in evn)
            {
                comboBox1.Items.Add(kv.Key);
            }
            comboBox1.SelectedIndex = 0;

            comboBox2.Items.Add("POST");
            comboBox2.Items.Add("GET");
            comboBox2.SelectedIndex = 0;

            comboBox3.Items.Add("FORM");
            comboBox3.Items.Add("JSON");
            comboBox3.SelectedIndex = 0;

            button1.Text = "请求";
            button2.Text = "保存请求";

            FlushReqList();
        }
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string baseUrl = evn[comboBox1.SelectedItem.ToString()];
            string reqStr = textBox1.Text;
            if(reqStr.Length <= 0)
            {
                MessageBox.Show("需要填写请求地址");
                return;
            }

            string tokenUrl = baseUrl + "TokenGenerator/getToken";
            string url = baseUrl + reqStr;
            string data = JsonTool.Text2FormParam(textBox2.Text);
            if(tbx_username.Text.Length <= 0)
            {
                MessageBox.Show("需要填写登录账号");
                return;
            }
            string name = tbx_username.Text;
            string funcName = GetFuncName(reqStr);
            string restr = "";
            if (comboBox3.SelectedItem.ToString().Equals("FORM"))
            {
                restr = MyTools.RestfullRequest.LimsPostFormUseToken(tokenUrl, url, name, funcName, data);
            }else if (comboBox3.SelectedItem.ToString().Equals("JSON"))
            {
                restr = MyTools.RestfullRequest.LimsPostJsonUseToken(tokenUrl, url, name, funcName, data);
            }
            textBox3.Text = JsonTool.FormatJsonStr(restr);
        }

        /// <summary>
        /// 获取请求种的方法的名字
        /// </summary>
        /// <param name="reqStr"></param>
        /// <returns></returns>
        private string GetFuncName(string reqStr)
        {
            int index = reqStr.IndexOf("/");
            int len = reqStr.Length;
            string funcName = reqStr.Substring(index + 1, len - index - 1);
            return funcName;
        }

        /// <summary>
        /// 保存请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Req req = new Req()
            {
                url = textBox1.Text,
                param = JsonTool.Text2FormParam(textBox2.Text),
                method = comboBox2.SelectedItem.ToString(),
                contType = comboBox3.SelectedItem.ToString()
            };

            if(!string.IsNullOrEmpty(req.url) && !string.IsNullOrEmpty(req.param))
            {
                bool v = TxtTool.SaveReq2Text(req);
                if (v)
                {
                    FlushReqList();
                    MessageBox.Show("保存成功");
                }
            }
            else
            {
                MessageBox.Show("请求地址或参数为空");
            }
        }

        /// <summary>
        /// 刷新请求列表
        /// </summary>
        private void FlushReqList()
        {
            comboBox4.Items.Clear();
            List<Req> reqs = TxtTool.LoadReqFromTxt();
            // 刷新请求列表
            foreach (Req req1 in reqs)
            {
                if (req1 != null)
                {
                    comboBox4.Items.Add(req1.url);
                }
            }
        }

        /// <summary>
        /// 切换请求列表，自动的带出请求，并填充请求框和参数框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            string url = comboBox4.SelectedItem.ToString();
            List<Req> reqs = TxtTool.LoadReqFromTxt();
            foreach(Req req in reqs)
            {
                if(req.url == url)
                {
                    textBox1.Text = req.url;
                    textBox2.Text = req.param;
                    textBox2.Text = JsonTool.FormatFormParam(req.param);
                }
            }
        }

        /// <summary>
        /// 每5秒钟触发一下，检查请求是否有增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            FlushReqList();
        }

        /// <summary>
        /// 从文件中删除无用的请求连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            int index = comboBox4.SelectedIndex;
            if(index != -1)
            {
                comboBox4.Items.RemoveAt(index);
                Boolean b = TxtTool.deleteReqFromTxt(index);
                if (b)
                {
                    FlushReqList();
                    comboBox4.Text = "";
                    MessageBox.Show("删除成功");
                }
            }
            else
            {
                MessageBox.Show("先选择要删除的项");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<Req> reqs = MyTools.TxtTool.LoadReqFromTxt();
            foreach(Req req in reqs)
            {
                listBox1.Items.Add(req.url);
            }
        }
    }
}
